package kpi.javatraining6.gnatiuk.longdivision;

import java.util.Scanner;

/**
 * 
 * @author Surp
 * 
 */
public class LongDivision {
	public static final String VERT_SLASH = "|";
	public static final String NEW_LINE = "\n";
	public static final String SPACE = " ";
	public static final String LINE = "-";
	public static final String EMPTY_ROW = "";

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

		System.out.print("Enter divided:");
		int d1 = in.nextInt();
		System.out.print("Enter divider:");
		int d2 = in.nextInt();

		LongDivision division = new LongDivision();
		System.out.println(division.divide(d1, d2));
	}

	/**
	 * divides two numbers
	 * 
	 * @param d1 - divided
	 * @param d2 - divider
	 * @return long division in String format
	 */
	public String divide(int d1, int d2) {

		String longDivision = genHead(d1, d2);
		String totalShift = SPACE;
		String currentShift = "";
		int[] digits = getDigits(d1);
		int minuend = 0;
		int difference=0;
		for (int i = 0; i < digits.length; i++) {
			minuend = 10 * minuend + digits[i];
			if (minuend >= d2) {
				longDivision += totalShift + minuend + NEW_LINE;
				int subtrahend = (minuend / d2) * d2;
				difference = minuend - subtrahend;
				
				int minuendShiftLength = getNumberLength(minuend);
				minuendShiftLength -= getNumberLength(difference);
				minuendShiftLength = (difference == 0) ? 1 : minuendShiftLength;
				currentShift = getSpacedRow(minuendShiftLength);
				
				longDivision += getSpacedRow(totalShift.length() - 1);
				longDivision += (-1) * subtrahend + NEW_LINE;
				longDivision += totalShift;
				longDivision += getLine(getNumberLength(subtrahend) + 1);
				longDivision += NEW_LINE;

				minuend=difference;
				totalShift += currentShift;
				currentShift = EMPTY_ROW;
			} 
		}
		longDivision += totalShift + difference;
		return longDivision;
	}

	/**
	 * generates head for long division
	 * 
	 * @param d1 - divided
	 * @param d2 - divider
	 * @return
	 */
	private String genHead(int d1, int d2) {
		String template = SPACE+"%d" + VERT_SLASH + "%d" + NEW_LINE;
		String head = String.format(template, d1, d2);

		int shiftLength = getNumberLength(d1) + 1;
		head += getSpacedRow(shiftLength);
		head += VERT_SLASH;
		head += getLine(getNumberLength(d2) + 1);
		head += NEW_LINE;
		head += getSpacedRow(shiftLength);
		head += VERT_SLASH;
		head += d1 / d2;
		head += NEW_LINE;

		return head;
	}

	/**
	 * counts digits of the number
	 * @param number
	 * @return
	 */
	private int getNumberLength(int number) {
		return Integer.toString(number).length();
	}

	/**
	 * getting spaced row 
	 * @param len - length of a target row
	 * @return
	 */
	private static String getSpacedRow(int len) {
		String res = EMPTY_ROW;

		for (int i = 0; i < len; i++) {
			res += SPACE;
		}
		return res;
	}

	/**
	 * getting line that consists of "-" character
	 * @param len - length of the line
	 * @return
	 */
	private static String getLine(int len) {
		String line = EMPTY_ROW;

		for (int i = 0; i < len; i++) {
			line += LINE;
		}
		return line;
	}

	/**
	 * getting array of digits of the number
	 * 
	 * @param numb
	 * @return array of digits that contains in the number
	 */
	private static int[] getDigits(int numb) {
		char[] arr = Integer.toString(numb).toCharArray();
		int[] digitsArray = new int[arr.length];

		for (int i = 0; i < arr.length; i++) {
			digitsArray[i] = Integer.parseInt(Character.toString(arr[i]));
		}
		return digitsArray;
	}
}
